<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}

if($_POST['sub']!=''){
    $id=empty($_POST['id'])?0:(int)$_POST['id'];
    $codenumb=trim($_POST["codenumb"]);
    $thistoken=md5(EZKEY.$codenumb.$_SESSION['EZ_Admin']['id']);
    if(empty($_POST["token"])){
       jumpajax('300','质检入库提交失败！',''); 
    }
    if(empty($id)){
       jumpajax('300','没有找到加工单，无法入库！','');  
    }
    if($thistoken==trim($_POST["token"])){
    if(empty($_POST["token"])){
       jumpajax('300','质检入库提交失败！！',''); 
    }
    }
    $setstatus=trim($_POST["setstatus"]);
    if($setstatus==""){
        
        jumpajax('300','没有设置成品入库状态，请检查数据！','');
    }
    $workinfo=$ezhand->select("workman",'*','id="'.$id.'"');
    if(empty($workinfo["orderid"])){
        error_html("没有找到加工单！","","");
    }
    if($workinfo["status"]<>2){
          jumpajax('300','加工单还没完成，请先确认加工完成！','');
    }
    if($workinfo["zjstatus"]<>2){
          jumpajax('300','加工单还没质检完成，请先确认质检完成！','');
    }
    $goodsnumlist=$_POST["goods"];
    if(!is_array($goodsnumlist)){
        jumpajax('300','入库数量必须是数值！','');
    }
    $checklist=$ezhand->select_all("workman_checklist","goodsid,title,goodssn,ordernum,sum(goodsnum) as goodsnum,sum(checknum) as checknum,sum(unchecknum) as unchecknum","workmanid=".$id." group by goodsid,title,goodssn,ordernum order by goodsid asc,id asc");
    $goodsinfo=array();
    while(list($k,$v)=each($checklist)){
        $gid=$v["goodsid"];
        $goodsinfo[$gid]=$v;
    }
    $havegoods=$ezhand->select_2_field("workman_putlist","goodsid,sum(putsnum) as putsnum","workmanid=".$id." group by goodsid");

    
    $t=time();
    $putlist=array();
    while(list($mygid,$thev)=each($goodsnumlist)){
        $thev=intval($thev);
        if($thev<0){
          jumpajax('300','入库数量必须大于0！','');  
        }
        if(!isset($havegoods[$mygid])){
            $havegoods[$mygid]=0;
        }
        if($havegoods[$mygid]+$thev>$goodsinfo[$mygid]["checknum"]){
            jumpajax('300','入库数量不得大于质检数量！',''); 
        }
        $row=array(
        "goodsid"=>$mygid,
        "title"=>$goodsinfo[$mygid]["title"],
        "goodssn"=>$goodsinfo[$mygid]["goodssn"],
        "ordernum"=>$goodsinfo[$mygid]["ordernum"],
        "putsnum"=>$thev,
        "workmanid"=>$id,
        "userid"=>$_SESSION['EZ_Admin']['id'],
        "username"=>$_SESSION['EZ_Admin']['name'],
        "ctime"=>$t
        );
        $putlist[]=$row;
    }
    if(!empty($putlist)){
        while(list($k,$pdata)=each($putlist)){
        
            $ezhand->insert("workman_putlist",$pdata);
        
        }
        $udata=array(
        "ddstatus"=>$setstatus,
        "dd_time"=>$t,
        "dd_username"=>$_SESSION['EZ_Admin']['name']
        );
        $ezhand->update("workman",$udata,"id=".$id);
         
        if($setstatus==1){
            $ezhand->workman_log($id,"116");
            jumpajax('200','加工单'.$codenumb.'部分入库完成，请尽快补充剩余量！','page394','closeCurrent');    
        }else{
            $od=array("shipstatus"=>6);
            $ezhand->update("shopping3_order",$od,"workid=".$id);

            $ezhand->workman_log($id,"117");
            jumpajax('200','加工单'.$codenumb.'入库完成！','page394','closeCurrent');

                  
        }
        

                
        
    }else{
         jumpajax('300','质检入库数据位空，请检查入库数据！','');
    }
    
    
    
}


$id=empty($_GET['id'])?0:(int)$_GET['id'];
if(empty($id)){

    error_html("没有找到加工单！","","");
}else{
    $workinfo=$ezhand->select("workman",'*','id="'.$id.'"');
    if(empty($workinfo["orderid"])){
        error_html("没有找到加工单！","","");
    }
    $codenumb=$workinfo["code_numb"];
    $ordernum=$workinfo["ordernum"];
    $havegoods=$ezhand->select_2_field("workman_putlist","goodsid,sum(putsnum) as putsnum","workmanid=".$id." group by goodsid");
   
    $checklist=$ezhand->select_all("workman_checklist","goodsid,title,goodssn,ordernum,sum(goodsnum) as goodsnum,sum(checknum) as checknum,sum(unchecknum) as unchecknum","workmanid=".$id." group by goodsid,title,goodssn,ordernum order by goodsid asc,id asc");
        
}

$token=md5(EZKEY.$codenumb.$_SESSION['EZ_Admin']['id']);
?>